﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TfsBacklogMgr.Main;

namespace NUnitTests
{
    [TestFixture]
    public class TestWorkItems
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            Factory.IsTesting = true;
            Factory.GetFactory().LoadSettings("dummy");
        }

        [TearDown]
        public void TearDown()
        {
            WorkItem wi = WorkItem.GetWorkItem(11);
            wi.IterationPath = "$/7.1/Stake/Sprint 1";

            wi = WorkItem.GetWorkItem(13);
            wi.IterationPath = "$/7.1/Stake/Backlog";
        }

  
        [Test]
        public void TestWorkitemUserStory()
        {
            WorkItem wi = WorkItem.GetWorkItem(11);
            Assert.AreEqual("UserStory", wi.WIT);
            Assert.AreEqual(11, wi.Id);
            Assert.AreEqual("christian", wi.AssignedTo.Login);
            Assert.AreEqual("Sale Archive", wi.Title);
            Assert.AreEqual("$/Sale", wi.AreaPath);
            Assert.AreEqual("$/7.1/Stake/Sprint 1", wi.IterationPath);
            Assert.AreEqual("$/7.1/Stake/Sprint 1", wi.Iteration.Path);
            Assert.AreEqual(8, wi.Points);
            Assert.AreEqual(0, wi.Hours);
            Assert.AreEqual(0, wi.Remain);

            Assert.Contains(wi, wi.Iteration.SprintChildren);
            Assert.Contains(wi, wi.Iteration.WorkItems);
            CollectionAssert.DoesNotContain(wi.Iteration.BacklogChildren, wi);

        }

        [Test]
        public void TestChangeIterationToBacklog()
        {
            WorkItem wi = WorkItem.GetWorkItem(11);
            Iteration orgIter = wi.Iteration;
            wi.IterationPath = "$/7.1/Stake";
            Iteration newIter = wi.Iteration;

            CollectionAssert.DoesNotContain(orgIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(orgIter.BacklogChildren, wi);
            CollectionAssert.DoesNotContain(orgIter.SprintChildren, wi);

            CollectionAssert.Contains(newIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(newIter.SprintChildren, wi);
            CollectionAssert.Contains(newIter.BacklogChildren, wi);
        }

        [Test]
        public void TestChangeIterationMovesChildren()
        {
            WorkItem wi = WorkItem.GetWorkItem(11);
            Iteration orgIter = wi.Iteration;
            wi.IterationPath = "$/7.1/Stake";
            Iteration newIter = wi.Iteration;

            CollectionAssert.Contains(newIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(newIter.SprintChildren, wi);
            CollectionAssert.Contains(newIter.BacklogChildren, wi);

            // check that children have been moved also
            foreach (WorkItem child in wi.Children)
            {
                if (!child.IsClosed)
                {
                    Assert.AreEqual(newIter, child.Iteration);
                    // child tasks are not in the iteration child lists, to avoid duplication in the tree
                    CollectionAssert.DoesNotContain(newIter.WorkItems, child);
                    CollectionAssert.DoesNotContain(newIter.SprintChildren, child);
                    CollectionAssert.DoesNotContain(newIter.BacklogChildren, child);
                }
            }
        }


        [Test]
        public void TestChangeIterationToSprint()
        {
            WorkItem wi = WorkItem.GetWorkItem(13);
            Iteration orgIter = wi.Iteration;
            wi.IterationPath = "$/7.1/Stake/Sprint 1";
            Iteration newIter = wi.Iteration;

            CollectionAssert.DoesNotContain(orgIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(orgIter.BacklogChildren, wi);
            CollectionAssert.DoesNotContain(orgIter.SprintChildren, wi);

            CollectionAssert.Contains(newIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(newIter.BacklogChildren, wi);
            CollectionAssert.Contains(newIter.SprintChildren, wi);
        }

        [Test]
        public void TestChangeIterationToSprintMovesChildrenAlso()
        {
            WorkItem wi = WorkItem.GetWorkItem(13);
            Iteration orgIter = wi.Iteration;
            wi.IterationPath = "$/7.1/Stake/Sprint 1";
            Iteration newIter = wi.Iteration;

            CollectionAssert.Contains(newIter.WorkItems, wi);
            CollectionAssert.DoesNotContain(newIter.BacklogChildren, wi);
            CollectionAssert.Contains(newIter.SprintChildren, wi);

            // check that children have been moved also
            foreach (WorkItem child in wi.Children)
            {
                if (!child.IsClosed)
                {
                    Assert.AreEqual(newIter, child.Iteration);
                    // child tasks are not in the iteration child lists, to avoid duplication in the tree
                    CollectionAssert.DoesNotContain(newIter.WorkItems, child);
                    CollectionAssert.DoesNotContain(newIter.SprintChildren, child);
                    CollectionAssert.DoesNotContain(newIter.BacklogChildren, child);
                }
            }
        }

    }
}
